﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SMS.ServiceManager;
using digit.Core;
using SMS.UserManager;

public partial class Modules_DetailSportTeam : WebControlBase
{
    private int teamId;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ClientState.IsAuthenticated)
        {
            Response.Redirect("~/Login.aspx?session=timeout");
        }

        teamId = Request.QueryString["teamId"] != null ? Int32.Parse(Request.QueryString["teamId"].ToString().Trim()) : -1;

        if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1)
        {
            if (teamId == -1)
            {
                lnkAdd.Enabled = false;
                lnkAdd.Enabled = false;
            }
        }

        if (IsPostBack) return;

        BindInfo();
    }

    protected void lnkAdd_Click(object sender, System.EventArgs e)
    {
        if (Page.IsValid)
        {
            DoSave();
        }
    }

    protected void lnkBack_Click(object sender, System.EventArgs e)
    {
        Response.Redirect("Default.aspx?module=ListSportTeam");
    }

    private void BindInfo()
    {
        SportTeamInfo objTeam = SportTeamManager.Select(teamId);

        if (objTeam != null)
        {
            this.txtTeamName.Text = objTeam.TeamName;
            this.ddlType.SelectedValue = objTeam.Type.ToString();
            this.txtTeamCode.Text = objTeam.TeamCode;
            this.txtTeamCodeOpen.Text = objTeam.TeamCodeOpen;
            this.txtContent1.Text = objTeam.Msg1;
            this.txtContent2.Text = objTeam.Msg2;
            this.txtContent3.Text = objTeam.Msg3;
            this.txtContent4.Text = objTeam.Msg4;
            this.txtContent5.Text = objTeam.Msg5;

            if (objTeam.Status == 1)
                this.chkStatus.Checked = true;
            else
                this.chkStatus.Checked = false;
        }
    }

    private bool DoSave()
    {
        try
        {
            bool result = false;
            if (teamId > 0)
            {
                SportTeamInfo objTeam = SportTeamManager.Select(teamId);
                if (objTeam != null)
                {
                    objTeam.TeamID = teamId;
                    objTeam.TeamName = this.txtTeamName.Text;
                    objTeam.TeamCode = this.txtTeamCode.Text;
                    objTeam.TeamCodeOpen = this.txtTeamCodeOpen.Text;
                    objTeam.Msg1 = this.txtContent1.Text.Trim();
                    objTeam.Msg2 = this.txtContent2.Text.Trim();
                    objTeam.Msg3 = this.txtContent3.Text.Trim();
                    objTeam.Msg4 = this.txtContent4.Text.Trim();
                    objTeam.Msg5 = this.txtContent5.Text.Trim();

                    objTeam.Type = byte.Parse(this.ddlType.SelectedValue);

                    if (chkStatus.Checked)
                        objTeam.Status = 1;
                    else
                        objTeam.Status = 0;

                    result = SportTeamManager.Update(objTeam);
                    if (result)
                        lblError.Text = "Cập nhật thành công!";
                    else
                        lblError.Text = "Cập nhật lỗi";
                }
            }
            else
            {
                SportTeamInfo objTeam = new SportTeamInfo();
                objTeam.TeamName = this.txtTeamName.Text;
                objTeam.TeamCode = this.txtTeamCode.Text;
                objTeam.TeamCodeOpen = this.txtTeamCodeOpen.Text;
                objTeam.Msg1 = this.txtContent1.Text.Trim();
                objTeam.Msg2 = this.txtContent2.Text.Trim();
                objTeam.Msg3 = this.txtContent3.Text.Trim();
                objTeam.Msg4 = this.txtContent4.Text.Trim();
                objTeam.Msg5 = this.txtContent5.Text.Trim();
                objTeam.Type = byte.Parse(this.ddlType.SelectedValue);

                if (chkStatus.Checked)
                    objTeam.Status = 1;
                else
                    objTeam.Status = 0;

                result = SportTeamManager.Insert(objTeam);
                if (result)
                    lblError.Text = "Cập nhật thành công!";
                else
                    lblError.Text = "Cập nhật lỗi";
            }
            return result;
        }
        catch
        {
            return false;
        }
    }
}
